--
-- Author: shunguo.chen
-- Date: 2017-12-02 13:08:17
--
-- PlayerExpBar 玩家经验条

local ClassRef = app.mvc.createPanelClass(...)
local PlayerExpTips = import(".PlayerExpTips")

-- 一整个经验条动画所需帧数
local FRAME_MAX = 30
local FRAME_MIN = 15

local curExp = 0
local nextExp = 0
local maxExp = 0
local curPer = 0
local nextPer = 0
local frameMax = 0
local frame = 0
local framePer = 0
local frameExp = 0

-- 本地化，加快访问速度
local numText
local bar
local barInLock
local Text_setString = ccui.Text.setString
local Bar_setPercent = ccui.LoadingBar.setPercent
local expTips
local barNew --浅紫色进度条
local barTmp --在浅紫色条到达终点后 浅紫色渐隐 bartmp渐出 最后设置当前的进度条

function ClassRef.create(node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.PlayerExpNode,true))
    if node == nil then
        display.doLayout(sprite)
    end
    sprite:setNodeEventEnabled(true)
    sprite:init()
    return sprite
end

function ClassRef:onExit()
    numText = nil
    bar = nil
    barInLock = nil
end

-- data = {level=, exp=, maxExp=}
function ClassRef:init(data)
    self.super.init(self, data)
    self._stepDatas = nil
    self._step = 0
    self._compCB = nil
    numText = self._ui.num
    self.worldExpIsLock = false
    bar = self._ui.power
    barNew = self._ui.power_2
    barTmp = self._ui.power_0

    barInLock = self._ui.power_1
    expTips = PlayerExpTips.create(self._ui.exp_tips)
    expTips:setExpBarWidth(bar:getContentSize().width)

    display.setCascadeOpacityEnabled(barNew,true)
    display.setCascadeColorEnabled(barNew,true)
    display.setCascadeOpacityEnabled(barTmp,true)
    display.setCascadeColorEnabled(barTmp,true)
    --TODO 暂时不显示
    barInLock:setVisible(false)
end

function ClassRef:setWorldLock(poolExp)
    self._poolExp = poolExp or 0
    self.worldExpIsLock = poolExp > 0
    barInLock:setVisible(poolExp > 0)
    if self.worldExpIsLock and self._data then
        Bar_setPercent(barInLock, (self._poolExp + self._data.exp) / self._data.maxExp * 100)
    end
end

function ClassRef:refresh()
    Text_setString(numText, string.format("%s/%s", self._data.exp, self._data.maxExp))
    if self.worldExpIsLock then
        Bar_setPercent(barInLock, (self._poolExp + self._data.exp) / self._data.maxExp * 100)
    end
    Bar_setPercent(bar, self._data.exp / self._data.maxExp * 100)
end

function ClassRef:showGetExp(datas, cb)
        bar:setVisible(true)

        barNew:setVisible(true)
        barNew:setOpacity(255)
        barTmp:setVisible(false)
        barTmp:setOpacity(0)
        Bar_setPercent(barNew, 0)
        Bar_setPercent(barTmp, 0)

    if self._stepDatas then
        for i=2,#datas do
            self._stepDatas[#self._stepDatas + 1] = datas[i]
        end
    else
        self._stepDatas = datas
        self._step = 0
        self:nextStep()
        self:scheduleUpdate(handler(self, self.tick))
    end

    self._data = datas[#datas]
    self._compCB = cb
end

function ClassRef:stepOver()
    self:refresh()
    if self._stepDatas then
        executeFunc(self._compCB, self._stepDatas)
        self:showExpTips(self._stepDatas)
        self._stepDatas = nil
        self:unscheduleUpdate()
    end
end

function ClassRef:showExpTips(stepDatas)
    if not stepDatas then return end
    expTips:setTips(stepDatas)
    --到最后一步的时候
    --浅紫变深紫色
    local precent = stepDatas[#stepDatas].exp/stepDatas[#stepDatas].maxExp * 100
    Bar_setPercent(barTmp, precent)
    barTmp:setOpacity(0)
    barTmp:setVisible(true)
    barNew:stopAllActions()
    barNew:runAction(cca.fadeOut(1))
    local act = cca.seq({cca.fadeIn(1),cca.callFunc(function()
        Bar_setPercent(bar, precent)
        bar:setVisible(true)
    end)})
    barTmp:stopAllActions()
    barTmp:runAction(act)
end

function ClassRef:nextStep()
    self._step = self._step + 1
    -- 1.最后一步了，结束
    if self._step == #self._stepDatas then
        self:stepOver()
        return
    end

    local curData = self._stepDatas[self._step]
    local nextData = self._stepDatas[self._step + 1]

    -- 2.倒数第二步时，如果最后一步exp=0，结束
    if nextData.exp == 0 then
        self:stepOver()
        return
    end

    maxExp = curData.maxExp
    curExp = curData.exp
    if curExp == maxExp then curExp = 0 end
    nextExp = nextData.exp
    curPer = curData.exp/curData.maxExp * 100
    if curPer == 100 then curPer = 0 end
    nextPer = nextData.exp/nextData.maxExp * 100
    if nextPer == 0 then nextPer = 100 end
    frameMax = math.max(math.ceil((nextPer - curPer) / 100 * FRAME_MAX), FRAME_MIN)
    framePer = (nextPer - curPer) / frameMax
    frameExp = (nextExp - curExp) / frameMax
    frame = 0
end

function ClassRef:tick(dt)
    frame = frame + 1
    if frame < frameMax then
        local num = curExp + math.round(frameExp * frame)
        Text_setString(numText, string.format("%s/%s", num, maxExp))

        Bar_setPercent(barNew, curPer + framePer * frame)

    elseif frame == frameMax then
        Text_setString(numText, string.format("%s/%s", nextExp, maxExp))

        Bar_setPercent(barNew, nextPer)
        if nextPer == 100 then
            bar:setVisible(false)
        end

        self:nextStep()
    end
end

function ClassRef:setData(data)
    self._data = data
end

return ClassRef